<template>
  <div class="reg_container">
    <div class="reg_box">
      <el-form
        class="regForm"
        :model="regFrom"
        :rules="regResult"
        status-icon
        ref="regFrom"
      >
        <p>注册</p>
        <el-divider></el-divider>

        <!-- 密码 -->
        <el-form-item prop="username">
          <el-input
            v-model="regFrom.username"
            autocomplete="off"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="pass">
          <el-input
            type="password"
            v-model="regFrom.pass"
            autocomplete="off"
            placeholder="登录密码"
          ></el-input>
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item prop="checkPass">
          <el-input
            type="password"
            v-model="regFrom.checkPass"
            autocomplete="off"
            placeholder="确认登录密码"
          ></el-input>
        </el-form-item>

        <!-- 手机号 -->
        <el-form-item prop="phone">
          <el-input
            v-model="regFrom.phone"
            autocomplete="off"
            placeholder="手机号码"
          ></el-input>
        </el-form-item>

        <!-- 短信验证码 -->
        <el-form-item prop="authCode">
          <el-input
            class="input_authCode"
            v-model="regFrom.authCode"
            autocomplete="off"
            placeholder="输入6位短信验证码"
          ></el-input>

          <!-- 发送验证码按钮 -->
          <el-button
            class="btn_authCode"
            type="primary"
          >发送验证码</el-button>
        </el-form-item>

        <!-- 同意条款复选框 -->
        <el-form-item>
          <el-checkbox
            label="我已阅读并同意"
            v-model="regFrom.trems"
          ></el-checkbox>
          <span class="span_text">《西安云海智创物联网开放平台服务条款》</span>
          <span>和</span>
          <span class="span_text">《西安云海智创物联网开放平台隐私政策》</span>
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item>
          <el-button
            style="width: 370px"
            type="primary"
            :disabled = !regFrom.trems
          >立即注册</el-button>

          <el-button
            style="width: 115px"
            @click="resetForm('regFrom')"
          >重置</el-button>
        </el-form-item>

        <div class="span_div">
          <span>已有账号？</span>
          <span class="span_text" @click="login">马上登录</span>
        </div>
      </el-form>
    </div>

  </div>

</template>

<script>
export default {
  data() {
    //初始密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.regFrom.checkPass !== "") {
          this.$refs.regFrom.validateField("checkPass");
        }
        callback();
      }
    };

    //确认密码
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.regFrom.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    //手机号验证
    var validatePhone = (rule, value, callback) => {
      const patter = new RegExp("^1[0-9]{10}$"); //简单验证11位手机号

      if (!patter.test(value)) {
        return callback(new Error("请输入正确格式的手机号！"));
      } else {
        callback(); //必须有此项回调，否则验证会一直不通过
      }
    };
    return {
      regFrom: {
        username: "", //用户名
        pass: "", //密码
        checkPass: "", //确认密码
        phone: "", //手机号
        authCode: "", //短信验证码
        trems: false, //条款
      },
      regResult: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        phone: [{ validator: validatePhone, trigger: "blur" }],
      },
    };
  },
  methods: {

    login(){
        this.$router.push({ path: "/login" }); //页面路由跳转
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang='less' scoped>
.reg_container {
  min-width: 1024px;
  min-height: 640px;
  height: 100%;
  background-color: #f9f8f8;
}
.reg_box {
  display: block;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  width: 538px;
  height: 583px;
  background-color: #f9f8f8;
  border-radius: 5px; // 圆角
  position: absolute;
  top: 50px;
  left: 0;
  right: 0;
  margin: auto;
  //   top: 40%;
  //   left: 50%;
  //   transform: translate(-50%, -50%); // 根据自己位置 以自己为长度位移

  .regForm {
    position: absolute;
    width: 100%;
    padding: 10px 20px;
    box-sizing: border-box; // 设置边框

    p {
      font-size: 14px;
      font-weight: bold;
    }
    .el-divider {
      margin-top: 10px;
    }

    .input_authCode {
      width: 290px;
      padding-right: 10px;
    }

    .btn_authCode {
      width: 197px;
    }
    .btn_reg {
      width: 372px;
    }
    .el-checkbox{
        color:#000;
    }
    .span_div {
      display: flex; // 弹性布局
      justify-content: flex-end; // 尾部对齐
      font-size: 15px;
      margin-bottom: 25px;
    }
    .span_text {
        cursor: pointer;
        color: #409eff;
      }
  }
}
</style>
